package MyServlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.ServiceException;
import model.Candidate;
import model.Comment;
import model.Function;
import model.Role;
import model.User;
import ServletService.Service;

/**
 * Servlet implementation class CrudUser
 */
public class CrudUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CrudUser() {
		super();
		// TODO Auto-generated constructor stub
	}

	private String option;
	private User user;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		option = request.getParameter("option");

		switch (option) {
		case "add": {

			String firstname = request.getParameter("firstname");
			String lastname = request.getParameter("lastname");
			String email = request.getParameter("email");
			String mobile = request.getParameter("mobile");
			String phone = request.getParameter("phone");
			String username;
			int lastNameSize = lastname.length();

			if (lastNameSize < 5) {
				username = lastname + firstname.substring(0, 6 - lastNameSize);
			} else {
				username = lastname.substring(0, 5) + firstname.charAt(0);
			}

			if (Service.getUserService().getUserByUsername(username) != null) {
				if (Service.getUserService().getUserByUsername(username)
						.getUserName() != null) {
					Random rn = new Random();
					username = username + rn.nextInt() % 1000;
				}
			}

			String password = request.getParameter("password");
			String functionAsString = request.getParameter("function");
			Function function = Function.valueOf(functionAsString);
			String roleAsString = request.getParameter("roles");
			Role role = Service.getRoleService().getRoleByName(roleAsString);
			List<Role> roles = new ArrayList<Role>();
			roles.add(role);

			user = new User();
			user.setFirstName(firstname);
			user.setLastName(lastname);
			user.setEmail(email);
			user.setMobileNo(mobile);
			user.setTelephoneNo(phone);
			user.setUserName(username);
			user.setPassword(password);
			user.setFun(function);
			user.setRoles(roles);
			user.setActive(true);

			Service.getUserService().insertUser(user);
			break;
		}
		case "update": {
			user = Service.getUserService().getUser(
					Long.parseLong(request.getParameter("id")));
			if (!request.getParameter("firstname").equals("")) {
				user.setFirstName(request.getParameter("firstname"));
			}

			if (!request.getParameter("lastname").equals("")) {
				user.setLastName(request.getParameter("lastname"));
			}

			if (!request.getParameter("email").equals("")) {
				user.setEmail(request.getParameter("email"));
			}

			if (!request.getParameter("mobileno").equals("")) {
				user.setMobileNo(request.getParameter("mobileno"));
			}

			if (!request.getParameter("telephoneno").equals("")) {
				user.setTelephoneNo(request.getParameter("telephoneno"));
			}

			if (!request.getParameter("password").equals("")) {
				user.setPassword(request.getParameter("password"));
			}

			String functionAsString = request.getParameter("function");
			Function function = Function.valueOf(functionAsString);
			String roleAsString = request.getParameter("roles");
			Role role = Service.getRoleService().getRoleByName(roleAsString);
			List<Role> roles = new ArrayList<Role>();
			roles.add(role);

			user.setFun(function);
			user.setRoles(roles);

			Service.getUserService().updateUser(user);
			
			
			break;
		}
		case "delete": {
			
			user = new User();
			user = Service.getUserService().getUser(
					Long.parseLong(request.getParameter("id")));
			user.setActive(false);
			Service.getUserService().updateUser(user);
			
			
			break;
		}
		case "getAll":{
			
			Service.getUserService().getAllUsers();
			break;
		}
		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/login");
		rd.forward(request, response);
	}

}
